package com.study.Thread;

import java.util.concurrent.CompletableFuture;

/**
 * jdk8以后还提供了CompletableFuture
 * 1.SupplyAsync开启任务
 * 2.ThenCompose连接两个任务将第一个任务执行完后的结果交给第二个任务由第二个任务返回结果
 * 3.ThenCombine用于合并多个任务，结果由合并函数BIfunction返回
 */
public class CompletableFutureTest {
    public static void main(String[] args) {
        SmallTool.printTimeAndThread("小白进入餐厅");
        SmallTool.printTimeAndThread("小白点了 番茄炒蛋+一碗白米饭");
//        CompletableFuture<String> future=CompletableFuture.supplyAsync(()->{
//            SmallTool.printTimeAndThread("厨师炒菜");
//            SmallTool.sleepMills(1000);
//            CompletableFuture future1=CompletableFuture.supplyAsync(()->{
//                SmallTool.printTimeAndThread("服务员打饭");
//                SmallTool.sleepMills(500);
//                return "+米饭";
//            });
//            return "番茄炒蛋"+future1.join();
//        });
        System.out.println("====================================");
//        CompletableFuture future=CompletableFuture.supplyAsync(()->{
//            SmallTool.printTimeAndThread("厨师炒菜");
//            SmallTool.sleepMills(1000);
//            return "番茄炒蛋";
//        }).thenCompose(cai->CompletableFuture.supplyAsync(()->{
//            SmallTool.printTimeAndThread("服务员打饭");
//            SmallTool.sleepMills(500);
//            return cai+"+白米饭";
//        }));
        CompletableFuture<String> future=CompletableFuture.supplyAsync(()->{
            SmallTool.printTimeAndThread("厨师炒番茄炒蛋");
            SmallTool.sleepMills(1000);
            return "番茄炒蛋";
        }).thenCombine(CompletableFuture.supplyAsync(()->{
            SmallTool.printTimeAndThread("服务员蒸饭");
            SmallTool.sleepMills(500);
            return "米饭";
        }),(dish,rice)->{
            SmallTool.printTimeAndThread("服务员打饭");
            SmallTool.sleepMills(100);
            return String.format("%s+%s好了",dish,rice);
        });
        SmallTool.printTimeAndThread("小白打王者");
        SmallTool.printTimeAndThread("饭好了，小白开吃"+future.join());
    }
}
